# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  # Pick a unique cookie name to distinguish our session data from others'

  session :session_key => '_untitled6_session_id'  
  before_filter :authorize
  
  protected
    # Override in controller classes that should require authentication
  def secure?
    false
  end
  def login_required
    if session[:usuario]
      return true
    end
    flash[:warning]='Por favor logue para continuar'
    session[:return_to]=request.request_uri
    redirect_to :controller => "main", :action => "login"
    return false 
  end
  def current_user
    session[:usuario]
  end
  def redirect_to_stored
    if return_to = session[:return_to]
      session[:return_to]=nil
      redirect_to(return_to)
    else
      redirect_to :controller=>'main', :action=>'index'
    end
  end
  private  
  def authorize  
    if secure? && session["matricula"].nil?    
      session["return_to"] = request.request_uri      
      redirect_to :controller => "main", :action => "login"   
      return false
    end
  end
end
